﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Configuration;
using System.Security.Cryptography;

namespace DAO
{
    public class ThanhVienDAO
    {
        #region 1   -   Queries
        public static ThanhVienDTO LayThanhVienTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            THANH_VIEN temp = myDB.THANH_VIENs.Single(c => c.ID == id);
            return ChuyenTuThanhVienSangThanhVienDTO(temp);
        }

        public static ThanhVienDTO LayThanhVienTheoUsername(string username)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            THANH_VIEN temp = myDB.THANH_VIENs.Single(c => c.Username == username);
            return ChuyenTuThanhVienSangThanhVienDTO(temp);
        }

        public static List<ThanhVienDTO> LayDanhSachThanhVienTheoUsername(string username)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var ListTV = from thanhvien in myDB.THANH_VIENs
                         where thanhvien.Username.Contains(username) == true
                         select thanhvien;

            List<ThanhVienDTO> result = new List<ThanhVienDTO>();
            foreach (THANH_VIEN item in ListTV)
            {
                ThanhVienDTO temp = ChuyenTuThanhVienSangThanhVienDTO(item);
                result.Add(temp);
            }
            return result;
        }

        public static List<ThanhVienDTO> LayDanhSachThanhVienTheoMaNhomThanhVien(int manhomthanhvien)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var ListTV = from thanhvien in myDB.THANH_VIENs
                         where thanhvien.MaNhomThanhVien == manhomthanhvien
                         select thanhvien;

            List<ThanhVienDTO> result = new List<ThanhVienDTO>();
            foreach (THANH_VIEN item in ListTV)
            {
                ThanhVienDTO temp = ChuyenTuThanhVienSangThanhVienDTO(item);
                result.Add(temp);
            }
            return result;
        }

        public static int DemSoLuongThanhVien()
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            return myDB.THANH_VIENs.Count();

        }
        public static bool CapNhatThanhVien(ThanhVienDTO thanhvienDTO)
        {            
            
            try
            {
                MyDataBaseDataContext myDB = new MyDataBaseDataContext();
                THANH_VIEN thanhvien = myDB.THANH_VIENs.Single(p => p.ID == thanhvienDTO.ID);
                thanhvien.NgaySinh = thanhvienDTO.NgaySinh;
                thanhvien.Password = thanhvienDTO.Password;
                thanhvien.SDT = thanhvienDTO.SDT;
                thanhvien.SoCMND = thanhvienDTO.SoCMND;
                thanhvien.DiaChi = thanhvienDTO.DiaChi;
                thanhvien.TenThanhVien = thanhvienDTO.TenThanhVien;
                thanhvien.MaNhomThanhVien = thanhvienDTO.MaNhomThanhVien;
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool ThemThanhVien(ThanhVienDTO thanhvienDTO)
        {
            THANH_VIEN temp = ChuyenTuThanhVienDTOSangThanhVien(thanhvienDTO);
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            try
            {
                myDB.THANH_VIENs.InsertOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool KiemTraUserNameDaTonTai(string username)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            try
            {
                THANH_VIEN thanhvien = myDB.THANH_VIENs.Single(c => c.Username == username);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool XoaThanhVienTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            THANH_VIEN temp = myDB.THANH_VIENs.Single(c => c.ID == id);
            try
            {
                myDB.THANH_VIENs.DeleteOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool XoaThanhVienTheoUserName(string username)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            THANH_VIEN temp = myDB.THANH_VIENs.Single(c => c.Username == username);
            try
            {
                myDB.THANH_VIENs.DeleteOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static string Encrypt(string toEncrypt, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
            // Get the key from config file
            string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
            //System.Windows.Forms.MessageBox.Show(key);
            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                hashmd5.Clear();
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            tdes.Clear();
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        public static string Decrypt(string cipherString, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = Convert.FromBase64String(cipherString);

            System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
            //Get your key from config file to open the lock!
            string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));

            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                hashmd5.Clear();
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            tdes.Clear();
            return UTF8Encoding.UTF8.GetString(resultArray);
        }

        #endregion

        #region 2   -   Function Helper

        public static ThanhVienDTO ChuyenTuThanhVienSangThanhVienDTO(THANH_VIEN thanhvien)
        {
            ThanhVienDTO result = new ThanhVienDTO();
            result.AnhDaiDien = thanhvien.AnhDaiDien;
            result.DiaChi = thanhvien.DiaChi;
            result.Email = thanhvien.Email;
            result.GioiTinh = thanhvien.GioiTinh.Value;
            result.ID = thanhvien.ID;
            result.MaNhomThanhVien = thanhvien.MaNhomThanhVien.Value;
            result.NgaySinh = thanhvien.NgaySinh.Value;
            //giải mã mật khẩu
            result.Password = Decrypt(thanhvien.Password,true);
            result.SDT = thanhvien.SDT;
            result.SoCMND = thanhvien.SoCMND;
            result.SoTienTrongTaiKhoan = thanhvien.SoTienTrongTaiKhoan.Value;
            result.TenThanhVien = thanhvien.TenThanhVien;
            result.TinhTrangThanhVien = thanhvien.TinhTrangThanhVien.Value;
            result.Username = thanhvien.Username;
            return result;
        }

        public static THANH_VIEN ChuyenTuThanhVienDTOSangThanhVien(ThanhVienDTO thanhvien)
        {
            THANH_VIEN result = new THANH_VIEN();
            result.AnhDaiDien = thanhvien.AnhDaiDien;
            result.DiaChi = thanhvien.DiaChi;
            result.Email = thanhvien.Email;
            result.GioiTinh = thanhvien.GioiTinh;
            result.ID = thanhvien.ID;
            result.MaNhomThanhVien = thanhvien.MaNhomThanhVien;
            result.NgaySinh = thanhvien.NgaySinh;
            result.Password = thanhvien.Password;
            result.SDT = thanhvien.SDT;
            result.SoCMND = thanhvien.SoCMND;
            result.SoTienTrongTaiKhoan = thanhvien.SoTienTrongTaiKhoan;
            result.TenThanhVien = thanhvien.TenThanhVien;
            result.TinhTrangThanhVien = thanhvien.TinhTrangThanhVien;
            result.Username = thanhvien.Username;
            return result;
        }
        #endregion
    }
}
